'use strict';

const Service = require('egg').Service;

class LinkService extends Service {

  // 新增
  async create(Link) {
    const { app } = this;
    return await app.model.Link.create(Link);
  }

  // 获取列表
  async getList(pageNum, pageSize) {
    const { app } = this;
    try {
      const offset = (pageNum - 1) * pageSize;
      const { count, rows } = await app.model.Link.findAndCountAll({
        // attributes: [
        //   'id',
        //   'name',
        //   'url',
        //   'pic',
        //   'stutas',
        // ],
        offset,
        limit: pageSize * 1,
      });
      return {
        pageNum,
        pageSize,
        total: count,
        list: rows,
      };
    } catch (error) {
      console.error(error);
      return null;
    }
  }

  // 删除
  async del(payload) {
    const { app } = this;
    return await app.model.Link.destroy({ where: { id: payload.ids }, force: true });
  }

  // 修改
  async updated(id, payload) {
    const { app } = this;
    return await app.model.Link.update(payload, { where: { id } });
  }

  // 根据id获取信息
  async getInfo(id) {
    return await this.app.model.Link.findByPk(id);
  }

}

module.exports = LinkService;
